//------------------------------------------------------------------------------
//
//       Grass.glsl
//
//    Creates 'PointSprite' like grass (billboarded)
//
//------------------------------------------------------------------------------

-- VS


layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;


out block {
  vec3 color;
} OUT;


void main()
{
  OUT.color = inColor;
  gl_Position = vec4(inPosition,1.0f);
}


--

//------------------------------------------------------------------------------


-- GS


layout(points) in;
layout(triangle_strip) out;
layout(max_vertices = 4) out;

in block{
  vec3 color;
} IN[];

out block{
  vec3 color;
  vec2 texCoord;
} OUT;

uniform mat4 uViewProjMatrix;
uniform vec3 uEyePosition;
uniform float uElapsedTime;

const float kParticleSize = 0.45f;

void main()
{
  // Optionally, send NBT for proper rotation
  
  mat4 mvp = uViewProjMatrix;  
  vec3 pos = gl_in[0].gl_Position.xyz;  
  vec3 toEye = (uEyePosition - pos);
  float dist = length(toEye);
  
  toEye = normalize(toEye);  
  vec3 up = (kParticleSize) * vec3( 0.0f, 1.0f, 0.0f); // -IN[0].normal;
  vec3 right = cross( up, toEye); //IN[0].tangent

  
  //--------------------

  OUT.color = IN[0].color; 
  
  vec3 topLeft = pos + up - right;
  gl_Position = mvp * vec4( topLeft, 1.0f);
  OUT.texCoord = vec2( 0.0f, 1.0f);
  EmitVertex();
  
  vec3 bottomLeft = pos - up - right;
  gl_Position = mvp * vec4( bottomLeft, 1.0f);
  OUT.texCoord = vec2( 0.0f, 0.0f);
  EmitVertex();
  
  vec3 topRight = pos + up + right;
  gl_Position = mvp * vec4( topRight, 1.0f);
  OUT.texCoord = vec2( 1.0f, 1.0f);
  EmitVertex();
  
  vec3 bottomRight = pos - up + right;
  gl_Position = mvp * vec4( bottomRight, 1.0f);
  OUT.texCoord = vec2( 1.0f, 0.0f);
  EmitVertex();
    
  EndPrimitive();
}


--

//------------------------------------------------------------------------------


-- FS


in block {
  vec3 color;
  vec2 texCoord;
} IN;

layout(location=0) out vec4 fragColor;

uniform sampler2D uDiffuseTex;


void main()
{
  fragColor = texture( uDiffuseTex, IN.texCoord);
  fragColor.rgb *= IN.color;
  fragColor.a = fragColor.r*fragColor.g*fragColor.b;
  //if (fragColor.a < 0.25f) { discard; }
}

